package cn.imvc.app.models;

import java.util.Date;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;

import cn.imvc.app.entities.Goods;
import cn.imvc.app.entities.GoodsLimitTime;
import cn.imvc.app.entities.Red;
import cn.imvc.app.repositories.GoodsLimitTimeRepository;
import cn.imvc.core.ModelUtil;
import cn.imvc.data.domain.ViewRequest;

@Service
public class GoodsLimitTimeModelImpl implements GoodsLimitTimeModel{

	@Autowired
	GoodsLimitTimeRepository GoodsLimitTimeRepository;
	
	public Page<GoodsLimitTime> find(ViewRequest viewRequest) {
		
		return GoodsLimitTimeRepository.findAll(ModelUtil.OrderByIdDesc(viewRequest));
	}

	public int save(GoodsLimitTime goodsLimitTime) {
		Goods goods = goodsLimitTime.getGoods();
		int goodsId = goods.getId();
		List<GoodsLimitTime> gg = GoodsLimitTimeRepository.getByGoodsId(goodsId);
		if(gg.size()>0){
			return -1;
		}
		GoodsLimitTimeRepository.saveAndFlush(goodsLimitTime);
		return 1;
	}

	public int update(int id,GoodsLimitTime goodsLimitTime) {
		GoodsLimitTime loadGoodsLimitTime = GoodsLimitTimeRepository.getOne(id);
		Goods goods = goodsLimitTime.getGoods();
		int goodsId = goods.getId();
		List<GoodsLimitTime> gg = GoodsLimitTimeRepository.getByGoodsId(goodsId);
		if(gg.size()>0&&!goods.equals(loadGoodsLimitTime.getGoods())){
			return -1;
		}
		BeanUtils.copyProperties(goodsLimitTime, loadGoodsLimitTime, new String[]{"id"});
		GoodsLimitTimeRepository.saveAndFlush(loadGoodsLimitTime);
		return 1;
	}

	public void delete(int id) {
		GoodsLimitTimeRepository.delete(id);
	}

	public GoodsLimitTime get(int id) {
		
		return GoodsLimitTimeRepository.getOne(id);
	}

}
